#include "producer.h"
#include "auth.hpp"

CProducerImpl::CProducerImpl(void)
{
}
CProducerImpl::~CProducerImpl(void)
{
    logi("CProducerImpl::~CProducerImpl");
}
HRESULT CProducerImpl::SetId(unsigned int id)
{
    m_id = id;
    return S_OK;
}
HRESULT CProducerImpl::Bind(LPCSTR addr, int len)
{
    HRESULT hr = S_OK;

    hr = this->ConnectChannel(addr, len, BUS_OP_CONNCET);
    rc_error(hr == S_OK, E_FAIL);

    // m_sem.Wait();

    return S_OK;
}
HRESULT CProducerImpl::Close()
{
    return this->CloseChannel();
}
HRESULT CProducerImpl::Send(IBase* pBase)
{
    return S_OK;
}
HRESULT CProducerImpl::OnComplete(IBase* pOperation, crtid cid, crterr err)
{
    HRESULT hr = S_OK;

    if (err == S_SUCCESS) {
        switch (cid) {
        case BUS_OP_CONNCET:
            hr = OnConnected(pOperation);
            break;
        case BUS_OP_SEND_SEQ:
            hr = OnSendSeqed(pOperation);
            break;
        case BUS_OP_RECV_SEQ_ACK:
            hr = OnRecvSeqAcked(pOperation);
            break;
        case BUS_OP_SEND_ACK:
            hr = OnSendAcked(pOperation);
            break;
        case BUS_OP_SEND_HEAD:
            hr = OnSendHeaded(pOperation);
            break;
        case BUS_OP_SEND:
            hr = OnSended(pOperation);
            break;
        default:
            loge("CProducerImpl::OnComplete");
            break;
        }
    }
    m_sem.Post();
    return S_OK;
}

HRESULT CProducerImpl::OnConnected(IBase* pIBase)
{
    HRESULT hr = S_OK;

    buf_ptr ptr = NULL;
    // hr = this->GetWritePtr(&ptr);
    rc_error(hr == S_OK, E_FAIL);

    hr = BusProtocol_Init_Head(ptr, m_id, 0, NDC_PRODUCER, BUS_CONSUMER_VERSION);
    rc_error(hr == S_OK, E_FAIL);

    hr = BusProtocol_1st(ptr, BUS_SEQ_VALUE, BUS_NULL_VALUE);
    rc_error(hr == S_OK, E_FAIL);

    // hr = this->OnWriteChannel(0, BUS_PROTOCOL_HEAD_LEN, BUS_OP_SEND_SEQ);
    rc_error(hr == S_OK, E_FAIL);

    return hr;
}

HRESULT CProducerImpl::OnSendSeqed(IBase* pIBase)
{
    HRESULT hr = S_OK;

    // hr = this->OnReadChannel(0, BUS_PROTOCOL_HEAD_LEN, BUS_OP_RECV_SEQ_ACK);
    rc_error(hr == S_OK, E_FAIL);

    return hr;
}

HRESULT CProducerImpl::OnRecvSeqAcked(IBase* pIBase)
{
    HRESULT hr = S_OK;

    buf_len TransferedBytes = 0;
    buf_ptr ptr = NULL;

    unsigned int s = 0;
    unsigned int a = 0;

    hr = BusProtocol_Get_Seq_Ack(ptr, &s, &a);
    rc_error(hr == S_OK, E_FAIL);

    // logi("c_recv==>seq:%u,ack:%u", a, s);

    hr = BusProtocol_3rd(ptr, s, a);
    rc_error(hr == S_OK, E_FAIL);

    // hr = this->WriteStream(ptr, 0, BUS_PROTOCOL_HEAD_LEN);
    rc_error(hr == S_OK, E_FAIL);

    // hr = this->OnWriteChannel(0, BUS_PROTOCOL_HEAD_LEN, BUS_OP_SEND_ACK);
    rc_error(hr == S_OK, E_FAIL);

    return hr;
}
HRESULT CProducerImpl::OnSendAcked(IBase* pIBase)
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CProducerImpl::OnSendHeaded(IBase* pIBase)
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CProducerImpl::OnSended(IBase* pIBase)
{
    HRESULT hr = S_OK;

    return hr;
}
